package com.my.commonlib.net.okgo;

import android.app.Activity;
import android.content.Context;
import android.view.ViewGroup;

import com.my.commonlib.util.Logger;
import com.my.commonlib.widget.LoadingView;

public class HttpRequestBase {
	

	private Context context;
	private boolean showLoadingProgress;
	private LoadingView loadingView;

	public HttpRequestBase(){
		showLoadingProgress = true;
	}
	
	public HttpRequestBase(Context context){
		showLoadingProgress = true;
		this.context = context;
	}
	
	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	public boolean isShowLoadingProgress() {
		return showLoadingProgress;
	}

	public void setShowLoadingProgress(boolean showLoadingProgress) {
		this.showLoadingProgress = showLoadingProgress;
	}
	
	public void startLoadingProgress(){
		Logger.i("======showLoadingProgress=" + showLoadingProgress + "===contxt== " + (context != null));
		if(showLoadingProgress && context != null){
			//Logger.i("======showLoadingProgress="+showLoadingProgress+"===contxt== "+ context != null);
			ViewGroup rootView = (ViewGroup)((Activity)context).getWindow().getDecorView().findViewById(android.R.id.content);
			if(rootView.getChildAt(rootView.getChildCount()-1) instanceof LoadingView){
				return;
			}
			loadingView = new LoadingView(context);

			rootView.addView(loadingView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
		}
	}
	
	public void dismissLoadingprogress(){
		if(showLoadingProgress && context != null && null != loadingView){
//			ViewGroup rootView = (ViewGroup)((Activity)context).getWindow().getDecorView().findViewById(android.R.id.content);
//			//loadingView.dismiss();
//			rootView.removeView(loadingView);
			loadingView.hide(context);
		}
	}

	/**
	 * 设置loadingview 的值
	 * @param text
	 */
	public void setResutText(String text){
		Logger.i("-----loadiing--->"+loadingView+"---showpro---->"+showLoadingProgress+"----text--->"+text);
		if(loadingView != null && showLoadingProgress){
//			loadingView.setResultText(text + "");
		}
	}


}
